package com.example.timessquare;

import com.squareup.timessquare.CalendarCellDecorator;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.DefaultDayViewAdapter;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;


/**
 * MainAbility
 */
public class MainAbility extends Ability {
    private static final String TAG = "SampleTimesSquareActivi";
    private CalendarPickerView calendar;
    private CommonDialog theDialog;
    private CalendarPickerView dialogView;
    private final Set<Button> modeButtons = new LinkedHashSet<Button>();
    private ShapeElement btnSelectedBgElement = new ShapeElement();
    private Button highlight;
    private Button single;
    private Button multi;
    private Button range;
    private Button displayOnly;
    private Button dialog;
    private Button customized;
    private Button decorator;
    private Button hebrew;
    private Button arabic;
    private Button arabicDigits;
    private Button customView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_sample_calendar_picker);
        btnSelectedBgElement.setRgbColor(new RgbColor(0x666666ff));

        final Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);

        final Calendar lastYear = Calendar.getInstance();
        lastYear.add(Calendar.YEAR, -1);
        initButtonListeners(nextYear, lastYear);
    }

    private void initButtonListeners(final Calendar nextYear, final Calendar lastYear) {
        calendar = (CalendarPickerView) findComponentById(ResourceTable.Id_calendar_view);
        single = (Button) findComponentById(ResourceTable.Id_button_single);
        multi = (Button) findComponentById(ResourceTable.Id_button_multi);
        highlight = (Button) findComponentById(ResourceTable.Id_button_highlight);
        range = (Button) findComponentById(ResourceTable.Id_button_range);
        displayOnly = (Button) findComponentById(ResourceTable.Id_button_display_only);
        dialog = (Button) findComponentById(ResourceTable.Id_button_dialog);
        customized = (Button) findComponentById(ResourceTable.Id_button_customized);
        decorator = (Button) findComponentById(ResourceTable.Id_button_decorator);
        hebrew = (Button) findComponentById(ResourceTable.Id_button_hebrew);
        arabic = (Button) findComponentById(ResourceTable.Id_button_arabic);
        arabicDigits = (Button) findComponentById(ResourceTable.Id_button_arabic_with_digits);
        customView = (Button) findComponentById(ResourceTable.Id_button_custom_view);

        modeButtons.addAll(Arrays.asList(single, multi, range, highlight, displayOnly, decorator, customView));
        setButtonsEnabled(decorator);


        setclick1(nextYear, lastYear);
        setclick3(nextYear, lastYear);
        setclick2(nextYear, lastYear);
    }

    private void setclick1(Calendar nextYear, Calendar lastYear) {
        single.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setButtonsEnabled(single);
                single.setBackground(btnSelectedBgElement);
                calendar.setCustomDayView(new DefaultDayViewAdapter());
                calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
                calendar.init(lastYear.getTime(), nextYear.getTime()) //
                        .inMode(CalendarPickerView.SelectionMode.SINGLE) //
                        .withSelectedDate(new Date());
            }
        });

        multi.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setButtonsEnabled(multi);
                calendar.setCustomDayView(new DefaultDayViewAdapter());
                Calendar today = Calendar.getInstance();
                ArrayList<Date> dates = new ArrayList<Date>();
                for (int i = 0; i < 5; i++) {
                    today.add(Calendar.DAY_OF_MONTH, 3);
                    dates.add(today.getTime());
                }
                calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
                calendar.init(new Date(), nextYear.getTime()) //
                        .inMode(CalendarPickerView.SelectionMode.MULTIPLE) //
                        .withSelectedDates(dates);
            }
        });

        highlight.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                setButtonsEnabled(highlight);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());

                MainAbility.this.calendar.setCustomDayView(new DefaultDayViewAdapter());
                MainAbility.this.calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
                // 20 years, enough to show performance failure.
                MainAbility.this.calendar.init(getDateWithYear(2000), getDateWithYear(2022))
                        .inMode(CalendarPickerView.SelectionMode.SINGLE).withSelectedDate(calendar.getTime());

                MainAbility.this.calendar.highlightDates(getHighlightedDaysForMonth( // Adds some highlighted days
                        calendar.get(Calendar.MONTH) - 1, calendar.get(Calendar.MONTH), calendar.get(Calendar.MONTH) + 1));
            }
        });


    }

    private void setclick2(final Calendar nextYear, final Calendar lastYear) {
        customized.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                showCalendarInDialog("Pimp my calendar!", ResourceTable.Layout_dialog_customized);
                dialogView.init(lastYear.getTime(), nextYear.getTime()).withSelectedDate(new Date());
            }
        });

        decorator.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setButtonsEnabled(decorator);

                calendar.setCustomDayView(new DefaultDayViewAdapter());
                calendar.setDecorators(Arrays.<CalendarCellDecorator>asList(new SampleDecorator()));
                calendar.init(lastYear.getTime(), nextYear.getTime()) //
                        .inMode(CalendarPickerView.SelectionMode.SINGLE) //
                        .withSelectedDate(new Date());
            }
        });

        hebrew.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                showCalendarInDialog("I'm Hebrew!", ResourceTable.Layout_dialog);
                dialogView.init(lastYear.getTime(), nextYear.getTime(), new Locale("iw", "IL")) //
                        .withSelectedDate(new Date());
            }
        });

        arabic.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                showCalendarInDialog("I'm Arabic!", ResourceTable.Layout_dialog);
                dialogView.init(lastYear.getTime(), nextYear.getTime(), new Locale("ar", "EG")) //
                        .withSelectedDate(new Date());
            }
        });

        arabicDigits.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                showCalendarInDialog("I'm Arabic with Digits!", ResourceTable.Layout_dialog_digits);
                dialogView.init(lastYear.getTime(), nextYear.getTime(), new Locale("ar", "EG")) //
                        .withSelectedDate(new Date());
            }
        });

        customView.setClickedListener(new Component.ClickedListener() {

            @Override
            public void onClick(Component component) {
                setButtonsEnabled(customView);
                calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
                calendar.setCustomDayView(new SampleDayViewAdapter());
                calendar.init(lastYear.getTime(), nextYear.getTime())
                        .inMode(CalendarPickerView.SelectionMode.SINGLE)
                        .withSelectedDate(new Date());
            }
        });
    }

    private void setclick3(Calendar nextYear, Calendar lastYear) {
        range.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                setButtonsEnabled(range);

                calendar.setCustomDayView(new DefaultDayViewAdapter());
                Calendar today = Calendar.getInstance();
                ArrayList<Date> dates = new ArrayList<Date>();
                today.add(Calendar.DATE, 3);
                dates.add(today.getTime());
                today.add(Calendar.DATE, 5);
                dates.add(today.getTime());
                calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
                calendar.init(new Date(), nextYear.getTime()) //
                        .inMode(CalendarPickerView.SelectionMode.RANGE) //
                        .withSelectedDates(dates);
            }
        });

        displayOnly.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                setButtonsEnabled(displayOnly);

                calendar.setCustomDayView(new DefaultDayViewAdapter());
                calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList());
                calendar.init(new Date(), nextYear.getTime()) //
                        .inMode(CalendarPickerView.SelectionMode.SINGLE) //
                        .withSelectedDate(new Date()) //
                        .displayOnly();
            }
        });

        dialog.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                String title = "I'm a dialog!";
                showCalendarInDialog(title, ResourceTable.Layout_dialog);
                dialogView.init(lastYear.getTime(), nextYear.getTime()) //
                        .withSelectedDate(new Date());
            }
        });
    }

    private void showCalendarInDialog(String title, int layoutResId) {
        dialogView = (CalendarPickerView) LayoutScatter.getInstance(getContext()).parse(layoutResId, null, false);
        theDialog = new CommonDialog(getContext())
                .setTitleText(title)
                .setContentCustomComponent(dialogView)
                .setButton(0, "确认", new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int i) {
                        theDialog.hide();
                    }
                });
        theDialog.setDestroyedListener(new CommonDialog.DestroyedListener() {
            @Override
            public void onDestroy() {
                dialogView.fixDialogDimens();
            }
        });
        theDialog.show();
    }

    private void setButtonsEnabled(Button currentButton) {
        for (Button modeButton : modeButtons) {
            modeButton.setEnabled(modeButton != currentButton);
            modeButton.setBackground(modeButton != currentButton ? null : btnSelectedBgElement);
        }
    }

    private Date getDateWithYear(int year) {
        final Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    private Date getDateWithYearAndMonthForDay(int year, int month, int day) {
        final Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    private List<Date> getHighlightedDaysForMonth(int... month) {
        List<Date> dateList = new ArrayList<>();

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        for (int i = 0; i < month.length; i++) {
            for (int j = 0; j < 25; j++) {
                dateList.add(getDateWithYearAndMonthForDay(c.get(Calendar.YEAR), i, j));
            }
        }

        return dateList;
    }
}